package com.lv.controller;





import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lv.common.AjaxResult;
import com.lv.common.Messages;
import com.lv.common.dto.OrderDto;
import com.lv.common.dto.OrderList;
import com.lv.common.dto.OrderMain;
import com.lv.entity.Order;

import com.lv.entity.OrderGoods;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-21
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{

    @PostMapping("/getAllOrder/{current}/{pageSize}")
    public AjaxResult getAllOrder(@PathVariable Integer current,@PathVariable Integer pageSize,@RequestParam String custName,@RequestParam String status){

        Page<OrderList> page = new Page<>(current, pageSize);
        IPage<OrderList> iPage = orderService.getAllOrder(page,custName,status);
        return AjaxResult.success(iPage);
    }

    @PostMapping("/getOrderMain/{id}")
    public AjaxResult getOrderMain(@PathVariable Long id){
        OrderMain main = orderService.getOrderMainById(id);
        return AjaxResult.success(main);
    }

    @PostMapping("/updateOrderStatus/{id}")
    public AjaxResult updateOrderStatus(@PathVariable Long id){
        Order order=new Order();
        order.setOrderId(id);
        order.setStatus(true);
        boolean b = orderService.updateById(order);
        if(b){
            return AjaxResult.success("结账成功");
        }
        return AjaxResult.error("结账失败");
    }

    @PostMapping("/getOrderById/{id}")
    public AjaxResult getOrderById(@PathVariable Long id){
        OrderDto order = orderMapper.getOrderById(id);
        return AjaxResult.success(order);

    }

    @PostMapping("saveOrder")
    public AjaxResult saveOrder(String order, String goods){
        Order o = JSON.parseObject(order, Order.class);
        List<OrderGoods> list = JSON.parseArray(goods, OrderGoods.class);
        int i = orderService.saveOrder(o, list);
        if(i==1) {
            return AjaxResult.success(Messages.SAVE_SUCCESS);
        }
        return AjaxResult.error(Messages.SAVE_ERROR);
    }

    @PostMapping("/removeOrder/{id}")
    public AjaxResult removeOrder(@PathVariable Long id){
        int i = orderService.removeOrder(id);
        if(i==1) {
            return AjaxResult.success(Messages.REMOVE_SUCCESS);
        }
        return AjaxResult.error(Messages.REMOVE_ERROR);
    }
    @PostMapping("updateOrder/{id}")
    public AjaxResult updateOrder(@PathVariable Long id,String goods){
        List<OrderGoods> list = JSON.parseArray(goods, OrderGoods.class);
        list.forEach(e->{
            e.setOrderId(id);
        });
        int i = orderService.updateOrder(id, list);
        if(i==1) {
            return AjaxResult.success(Messages.UPDATE_SUCCESS);
        }
        return AjaxResult.error(Messages.UPDATE_ERROR);
    }

    @PostMapping("/invoicing")
    public AjaxResult invoicing(Order order){
        boolean b = orderService.updateById(order);
        if(b){
            return AjaxResult.success(Messages.UPDATE_SUCCESS);
        }
        return AjaxResult.error(Messages.UPDATE_ERROR);
    }
    @PostMapping("/getOrderCount")
    public AjaxResult getOrderCount(){
        long count = orderService.count();
        return AjaxResult.success(count);
    }
    @PostMapping("/getAllUnpaidAmount")
    public AjaxResult getAllUp(){
        BigDecimal amount = orderMapper.getAllUnpaidAmount();
        return AjaxResult.success(amount);
    }
    @PostMapping("/getTrue")
    public AjaxResult getTrue(){
        BigDecimal aTrue = orderMapper.getTrue();
        return AjaxResult.success(aTrue);
    }

    @PostMapping("/getFalse")
    public AjaxResult getFalse(){
        BigDecimal aTrue = orderMapper.getFalse();
        return AjaxResult.success(aTrue);
    }
}

